package br.nextfm.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.nextfm.dao.UsuarioDao;
import br.nextfm.model.Administrador;
import br.nextfm.model.Usuario;

public class AutenticarUsuario extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession(false);
		
		String login = request.getParameter("login");
		String senha = request.getParameter("senha");
		
		UsuarioDao usuarioDao = new UsuarioDao();
		
		Usuario comum = usuarioDao.getUsuarioByLogin(login, senha);
		
		if (comum != null) {
			
			if (comum.getClass() == Administrador.class) {
				
				session.setAttribute("admin", comum);
				
			}
			
			session.setAttribute("usuario", comum);
			response.sendRedirect("index.do");
			
		}
		
		else {
			
			request.setAttribute("erroLogin", true);
			request.getRequestDispatcher("login.jsp").forward(request, response);
						
		}
		
	}

}
